package com.dingqb.myapplication;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Create by dingqb
 * On 2019-09-04
 * Description:
 */
public class BlurView extends View {
    public BlurView(Context context) {
        super(context);
    }

    public BlurView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    public BlurView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    Paint mPaint = new Paint();
    int x = 200;
    int y = 200;
    int radius = 100;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.e(TAG, "onMeasure: measureWidth=" + measureWidth);
        Log.e(TAG, "onMeasure: measureHeight=" + measureHeight);
        Log.e(TAG, "onMeasure: measureWidthMode=" + measureWidthMode);
        Log.e(TAG, "onMeasure: measureHeightMode=" + measureHeightMode);


        Log.e(TAG, "onMeasure: AT_MOST=" + MeasureSpec.AT_MOST);//->wrap_parent
        Log.e(TAG, "onMeasure: EXACTLY=" + MeasureSpec.EXACTLY);// 具体值
        Log.e(TAG, "onMeasure: UNSPECIFIED=" + MeasureSpec.UNSPECIFIED);//match_parent

        //        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    //对应11000000000000000000000000000000;总共32位，前两位是1
    static int MODE_MASK = 0xc0000000;

    //提取模式
    public static int getMode(int measureSpec) {
        return (measureSpec & MODE_MASK);
    }

    //提取数值
    public static int getSize(int measureSpec) {
        return (measureSpec & ~MODE_MASK);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(Color.RED);
        mPaint.setColor(Color.parseColor("#ff0000"));
        mPaint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.SOLID));
        canvas.drawCircle(x, y, radius, mPaint);
        Log.e(TAG, "onDraw: width=" + getWidth());
        Log.e(TAG, "onDraw: heigth=" + getHeight());
        Log.e(TAG, "onDraw: measurewidth=" + getMeasuredWidth());
        Log.e(TAG, "onDraw: measureheight=" + getMeasuredHeight());
        Log.e(TAG, "onDraw: left=" + getLeft());
        Log.e(TAG, "onDraw: top=" + getTop());
        Log.e(TAG, "onDraw: right=" + getRight());
        Log.e(TAG, "onDraw: bottom=" + getBottom());
        Log.e(TAG, "onDraw: getx==" + getX());
        Log.e(TAG, "onDraw: getY==" + getY());


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e(TAG, "onTouchEvent: getx" + event.getX());
        Log.e(TAG, "onTouchEvent: gety" + event.getY());
        Log.e(TAG, "onTouchEvent: getRawY" + event.getRawX());
        Log.e(TAG, "onTouchEvent: getRawY" + event.getRawY());
        return super.onTouchEvent(event);
    }

    public static final String TAG = "BlurView";
}
